package com.xyyl.patient.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.xyyl.patient.common.oss.AliyunCloudStorageService;
import com.xyyl.patient.core.util.ResponseBodyResult;
import com.xyyl.patient.vo.base.FileUploadRecord;

/**
 * @ClassName: AttachmentController
 * @author chens
 * @date 2017年8月17日
 *
 */
@RestController
@RequestMapping("/att")
public class AttachmentController {

	private Logger logger = LogManager.getLogger(AttachmentController.class);

	/**
	 * @Title: index
	 * @Description:文件上传
	 * @param multipartFile
	 *            参数
	 * @returnJSONObject 返回类型 @throws
	 */
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public JSONObject upload(@RequestParam(value = "file", required = true) MultipartFile multipartFile) {

		logger.info("upload file...");

		FileUploadRecord fileUploadRecord = null;

		try {

			if (multipartFile != null) {
				fileUploadRecord = AliyunCloudStorageService.upload(multipartFile);
				return ResponseBodyResult.success("success", fileUploadRecord);
			} else {
				return ResponseBodyResult.fail("file is empty");
			}
		} catch (Exception e) {

			logger.error("upload fai:{}", e);
			return ResponseBodyResult.error("文件上传失败!", e);
		}

	}

	/**
	 * @Title: index
	 * @Description:文件删除
	 * @param multipartFile
	 *            参数
	 * @return* JSONObject 返回类型 @throws
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public JSONObject delete(@RequestParam String key) {

		logger.info("delete file...");
		try {
			AliyunCloudStorageService.deleteFile(key);
			return ResponseBodyResult.success("success");
		} catch (Exception e) {

			logger.error("upload fai:{}", e);
			return ResponseBodyResult.error("文件删除失败!", e);
		}

	}

}
